package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.api.domain.CldProjectAccount;
import com.cleaningcloud.cld.domain.vo.CldRevisitVo;
import com.cleaningcloud.cld.mapper.CldProjectAccountMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldRevisitMapper;
import com.cleaningcloud.cld.domain.CldRevisit;
import com.cleaningcloud.cld.service.ICldRevisitService;

/**
 * 回访信息Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-06-12
 */
@Service
public class CldRevisitServiceImpl implements ICldRevisitService 
{
    @Autowired
    private CldRevisitMapper cldRevisitMapper;
    @Autowired
    private CldProjectAccountMapper cldProjectAccountMapper;

    /**
     * 查询回访信息
     * 
     * @param revisitId 回访信息主键
     * @return 回访信息
     */
    @Override
    public CldRevisitVo selectCldRevisitByRevisitId(Long revisitId)
    {
        return cldRevisitMapper.selectCldRevisitByRevisitId(revisitId);
    }

    /**
     * 查询回访信息列表
     * 
     * @param cldRevisitVo 回访信息
     * @return 回访信息
     */
    @Override
    public List<CldRevisitVo> selectCldRevisitList(CldRevisitVo cldRevisitVo)
    {
        return cldRevisitMapper.selectCldRevisitList(cldRevisitVo);
    }

    /**
     * 新增回访信息
     * 
     * @param cldRevisit 回访信息
     * @return 结果
     */
    @Override
    public int insertCldRevisit(CldRevisit cldRevisit)
    {
        cldRevisit.setCreateTime(DateUtils.getNowDate());
        cldRevisit.setCreateName(SecurityUtils.getUsername());
        cldRevisit.setCreateId(SecurityUtils.getUserId());
        cldRevisit.setFlag(0L);
        if(cldRevisit.getProjectAccountId() != null){
            CldProjectAccount projectAccount = cldProjectAccountMapper.selectCldProjectAccountByProjectAccountId(cldRevisit.getProjectAccountId());
            if(projectAccount != null){
                cldRevisit.setProjectAccountName(projectAccount.getUserName());
                cldRevisit.setProjectAccountPhone(projectAccount.getUserPhone());
            }
        }
        return cldRevisitMapper.insertCldRevisit(cldRevisit);
    }

    /**
     * 修改回访信息
     * 
     * @param cldRevisit 回访信息
     * @return 结果
     */
    @Override
    public int updateCldRevisit(CldRevisit cldRevisit)
    {
        if(cldRevisit.getProjectAccountId() != null){
            CldProjectAccount projectAccount = cldProjectAccountMapper.selectCldProjectAccountByProjectAccountId(cldRevisit.getProjectAccountId());
            if(projectAccount != null){
                cldRevisit.setProjectAccountName(projectAccount.getUserName());
                cldRevisit.setProjectAccountPhone(projectAccount.getUserPhone());
            }
        }
        return cldRevisitMapper.updateCldRevisit(cldRevisit);
    }

    /**
     * 批量删除回访信息
     * 
     * @param revisitIds 需要删除的回访信息主键
     * @return 结果
     */
    @Override
    public int deleteCldRevisitByRevisitIds(Long[] revisitIds)
    {
        return cldRevisitMapper.deleteCldRevisitByRevisitIds(revisitIds);
    }

    /**
     * 删除回访信息信息
     * 
     * @param revisitId 回访信息主键
     * @return 结果
     */
    @Override
    public int deleteCldRevisitByRevisitId(Long revisitId)
    {
        return cldRevisitMapper.deleteCldRevisitByRevisitId(revisitId);
    }
}
